package com.waves.design.singleton;

/**
 * 懒汉模式
 */
public class Singleton03 {

    public Singleton03() {
    }

    private static Singleton03 singleton03;

    /**
     * 多线程情况下不安全
     *
     * @return
     */
    public static Singleton03 getInstance() {
        if (singleton03 == null) {
            return new Singleton03();
        }
        return singleton03;
    }

    /**
     * 多线程情况下保护锁
     */
    public static synchronized Singleton03 getInstance02() {
        if (singleton03 == null) {
            return new Singleton03();
        }
        return singleton03;
    }

    /**
     * 上锁 性能更好 synchronize在方法中进行修饰 双重检查锁
     * 在多线程情况下也会发生指令重排的情况 故加个volatile修饰词
     */
    public static Singleton03 getInstance03() {
        if (singleton03 == null) {
            synchronized (Singleton03.class) {
                if (singleton03 == null) {
                    return new Singleton03();
                }
            }
        }
        return singleton03;
    }

}
